import Mock from 'mockjs'
import '@/mock/extend'
import {parseUrlParams} from '@/utils/request'
const Random = Mock.Random;
const source = Mock.mock({
  'list|21': [{
    'deviceName': () => Random.ctitle(2, 4),
    name:['温度(℃)', '密度(Kg/m³)', '粘度(Cst)', '介电常数(F/m)', '含水量(%)'],
    date:['2024/03/21 ', '2024/03/22 ', '2024/03/23', '2024/03/24', '2024/03/25', '2024/03/26', '2024/03/27'],
    series:[
      {
        name: '温度(℃)',
        type: 'line',
        stack: 'Total',
        symbol: 'none',
        data: [10, 20, 50, 20, 30, 1, 40]
      },
    {
      name: '密度(Kg/m³)',
      type: 'line',
      stack: 'Total',
      symbol: 'none',
      data: [20, 82, 1, 34, 20, 30, 30]
    },
    {
      name: '粘度(Cst)',
      type: 'line',
      stack: 'Total',
      symbol: 'none',
      data: [50, 22, 21, 14, 1, 33, 10]
    },
    {
      name: '介电常数(F/m)',
      type: 'line',
      stack: 'Total',
      symbol: 'none',
      data: [20, 32, 1, 34, 39, 30, 20]
    },
    {
      name: '含水量(%)',
      type: 'line',
      stack: 'Total',
      symbol: 'none',
      data: [20, 32, 1, 40, 90, 13, 30]
    }
  ]
   }]
});

Mock.mock(RegExp(`${process.env.VUE_APP_API_BASE_URL}/dataList` + '.*'),'get', ({url}) => {
  const params = parseUrlParams(decodeURI(url))
  let {page, pageSize} = params
  page = eval(page) - 1 || 0
  pageSize = eval(pageSize) || 10

  delete params.page
  delete params.pageSize

  let result = source.list.filter(item => {
    for (let [key, value] of Object.entries(params)) {
      if (item[key] !== value) {
        return false
      }
    }
    return true
  })
  const total = result.length
  if ((page) * pageSize > total) {
    result = []
  } else {
    result = result.slice(page * pageSize, (page + 1) * pageSize)
  }

  return {
    code: 0,
    message: 'success',
    data: {
      page: page + 1,
      pageSize,
      total: 100,
      list: result
    }
  }
})
